package com.zito.pojo;

import java.sql.Connection;
import java.util.Map;

import com.downlove.cache.CacheManager;
import com.downlove.common.Inflector;
import com.downlove.database.POJO;
import com.downlove.database.QueryHelper;

@SuppressWarnings("serial")
public class InvestansPOJO extends POJO {

	private Object[] valueObj;
	private String __this_table_name;

	public long Save(Connection conn) throws Exception {
		boolean flag = QueryHelper.update(conn, _InsertSQL(this), this.valueObj)>0;
		if (this.IsObjectCachedByID())
			CacheManager.evict(CacheRegion(), OBJ_COUNT_CACHE_KEY);
		return flag ? this.getId() : -1;
	}
	
	private String _InsertSQL(InvestansPOJO obj) {
		Map<String, Object> pojo_bean = obj.ListInsertableFields();
		String[] fields = pojo_bean.keySet().toArray(
				new String[pojo_bean.size()]);
		StringBuilder sql = new StringBuilder("INSERT INTO ");
		sql.append(obj.TableName());
		sql.append('(');
		valueObj = new Object[fields.length];
		for (int i = 0; i < fields.length; i++) {
			if (i > 0)
				sql.append(',');
			sql.append(fields[i]);
		}
		sql.append(") VALUES(");
		for (int i = 0; i < fields.length; i++) {
			if (i > 0)
				sql.append(',');
			sql.append('?');
			valueObj[i] = pojo_bean.get(fields[i]);
		}
		sql.append(')');
		return sql.toString();
	}

	protected String TableName() {
		if (__this_table_name == null)
			__this_table_name = Inflector.getInstance().tableize(getClass());
		return __this_table_name;
	}
}
